package com.fh.auth.portal;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.jasig.cas.authentication.handler.AuthenticationException;
import org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
import org.springframework.jdbc.core.JdbcTemplate;

public class PortalAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {
    /*	private DataSource dataSource;
     *
        public DataSource getDataSource() {
            return dataSource;
        }
        public void setDataSource(DataSource dataSource) {
            this.dataSource = dataSource;
        }*/
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    @Override
    protected boolean authenticateUsernamePasswordInternal(UsernamePasswordCredentials credentials) throws AuthenticationException {
        final String username = credentials.getUsername();
        final String password = credentials.getPassword();
        //个性化参数
        /*String sysCode = credentials.getSysCode();
        System.out.println("登录参数sysCode=="+sysCode);*/
        if (username != null && password != null && !"".equals(username) && !"".equals(password)) {
            String sql = " select t.DELETE_STATE,t.VALID_DATE from sys_user t where t.USER_CODE=? and t.`PASSWORD`=? ";
            Object[] param = new Object[2];
            param[0] = username;
            param[1] = password;
            List<Map<String, Object>> userList = getJdbcTemplate().queryForList(sql, param);
            if (userList != null && userList.size() > 0) {
                Map<String, Object> map = userList.get(0);
                if (map != null && map.size() > 0) {
                    String deleteState = String.valueOf(map.get("DELETE_STATE"));
                    if ("1".equals(deleteState)) {
                        throw new UserDeleteStateAuthenticationException();
                    }
                    if (!compareTo(String.valueOf(map.get("VALID_DATE")))) {
                        throw new UserValidDateAuthenticationException();
                    }
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 有效期验证
     *
     * @param validDate
     * @return true: 验证通过 ； false：验证失败
     */
    public static boolean compareTo(String validDate) {
        boolean result = false;
        if (validDate != null && !"null".equals(validDate) && !"".equals(validDate)) {
            java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
            java.util.Calendar nowTime = java.util.Calendar.getInstance();
            java.util.Calendar validTime = java.util.Calendar.getInstance();
            try {
                nowTime.setTime(df.parse(df.format(new Date())));
                validTime.setTime(df.parse(validDate));
                result = nowTime.compareTo(validTime) <= 0;
            } catch (java.text.ParseException e) {
                System.err.println("日期格式不正确");
            }
        }
        return result;
    }

}
